<template>
  <div class="common-layout">
    <el-container>
      <el-container>
        <el-aside width="200px"><NavMenu></NavMenu></el-aside>
        <el-container>
          <el-header height="60px"><Header></Header></el-header>
          <el-main>
          <el-card class="box-card" shadow="hover">
              <template #header>
                <div class="card-header">
                  <span>通知管理</span>
                </div>
              </template>
              <!--工具栏-->
            <div class="tool-nav">
              <el-input placeholder="搜索" v-model="searchStr" class="search" >
                <template #prepend>
                  <el-select v-model="select" placeholder="Select" style="width: 100px">
                    <el-option label="类型" value="type"/>
                    <el-option label="创建时间" value="create_time" />
                  </el-select>
                </template>
              </el-input>
              <el-button @click="fuzzySearch">
                <el-icon class="icon-button"><Search /></el-icon>
                <p>搜索</p>
              </el-button>
              <el-button @click="all">
                <el-icon class="icon-button"><refresh-right /></el-icon>
                <p>全部</p>
              </el-button>
              <el-button
                type="primary"
                @click="addNotice">
                <el-icon class="icon-button"><plus /></el-icon>
                <p>发布通知</p>
              </el-button>
            </div>
            <!--工具栏-->
            <!--数据-->
            <el-table :data="tableData" border style="width: 100%" v-loading="loading">
              <el-table-column prop="id" label="id" width="50" align="center"/>
              <el-table-column prop="type" label="类型" width="90" />
              <el-table-column prop="title" label="标题" width="170">
                <template v-slot="scope">
                  <el-tooltip placement="top" effect="light">
                    <template #content>{{scope.row.title}}</template>
                    <div class="oneLine">{{scope.row.title}}</div>
                  </el-tooltip>
                </template>
              </el-table-column>


              <el-table-column prop="content" label="内容">
                <template v-slot="scope">
                    <div class="oneLine">{{scope.row.content}}</div>
                </template>
              </el-table-column>

              <el-table-column prop="createTime" label="创建时间" width="180"/>
              <el-table-column label="操作"  width="70" align="center">
                <template #default="scope">
                  <edit class="edit" @click="handleEdit(scope.$index)"/>
                  <delete class="del" @click="myconfirm(scope.row)"/>
                </template>
              </el-table-column>
            </el-table>
            <!--数据-->
            <!--分页-->
            <div class="pagination">
              <el-pagination
                :page-sizes="[5, 10]"
                :background="background"
                layout="prev, pager, next"
                :total="total"
                :page-size="pageSize"
                :current-page="pageNO"
                @current-change="handleCurrentChange"
              /></div>
            <!--分页-->
            </el-card>
          </el-main>
        </el-container>
      </el-container>
    </el-container>
  </div>
</template>

<script>
import NavMenu from '@/components/NavMenu.vue'
import Header from '@/components/Header.vue'

export default {
  name: 'HomeView',
  components: {
    NavMenu,
    Header
  },
  data() {
    return {
      tableData: [],
      searchStr:'',
      select:'type',
      total:0,
      pageSize:7,
      pageNO:1,
      loading:true
    }
  },
  methods: {
    //跳转添加通知页面
    addNotice(){
      this.$router.push({
          name: "noticeadd",
      });
    },
    //跳转编辑通知页面
    handleEdit(index) {
		var that=this;
      this.$router.push({
          name:'noticeedit',
          params:{
              data:JSON.stringify(that.tableData[index]),
          }
      })
    },
    //加载全部通知信息
    loadNoticeInfo(){
		var that=this;
		var pageNO=this.pageNO;
		var pageSize=this.pageSize;
		this.axios
		.get("/notice/noticeList?pageNO="+pageNO+"&pageSize="+pageSize)
		.then(function (response) {
			console.log(response);
      that.tableData=response.data.rows;
      that.total=parseInt(response.data.total);
      that.loading=false;
		})
		.catch(function (error) {
      console.log(error);
		});

    },
    //换页 (全部/查询)
    handleCurrentChange(pageNO){
      this.loading=true;
      if(this.searchStr==""){
        this.pageNO=pageNO;
        this.loadNoticeInfo();
      }
      else{
        this.pageNO=pageNO;
        this.fuzzySearch();
      }
    },
    //修改/增加通知信息后更新表格数据
    updateData(){
      this.loading=true;
      this.loadNoticeInfo();
    },
    //删除通知信息
    myconfirm (index,row) {
        if(confirm('确定要删除吗')==true){
        this.handleDelete(index,row)
      }
    },
    handleDelete(row) {
      var that=this;
      this.loading=true;
      console.log(row.id)
      this.axios
        .get("/notice/del?id="+row.id)
        .then(function () {
          that.loadNoticeInfo();
        })
        .catch(function (error) {
            console.log(error);
        });
    },
    //模糊查询
    fuzzySearch(){
		this.loading=true;
		var that=this;
		let params = {
      pageNO:this.pageNO,
      pageSize:this.pageSize,
      searchStr:this.searchStr,
      select:this.select
		};
		this.axios
		.post("/notice/fuzzysearch", params)
		.then(function (response) {
      console.log(response);
      that.tableData=response.data.rows;
      that.total=parseInt(response.data.total);
      that.loading=false;
		})
		.catch(function (error) {
      console.log(error);
		});

    },
    //显示全部信息，并清空搜索框
    all(){
		this.loading=true;
		this.loadNoticeInfo();
		this.searchStr="";
    },
  },
  mounted(){
  this.loadNoticeInfo();
  }
}
</script>

<style scoped>
:deep(.el-header) { 
  padding: 0;
}
.box-card{
  height: 500px;
}
.card-header{
    float: left;
    line-height: 0px;
    color: var(--el-text-color-regular);
}
.tool-nav{
  display: flex;
  justify-content: left;
  margin-bottom: 20px;
}
.search{
  width: 290px;
  float: left;
  margin-right: 12px;
}
.icon-button{
  margin-right: 5px;
}

el-button, p{
  font-size: 13px;
}

.oneLine {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.edit{
  width: 1.2em;
  height: 1.2em;
  color: #1E90FF;
  margin-top: 3px;
  margin-right: 4px;
  margin-left: 4px;
  cursor:pointer;
}
.del{
  width: 1.2em;
  height: 1.2em;
  color: #ed1941;
  margin-top: 3px;
  cursor:pointer;
}
.el-pagination{
   
    justify-content: center;
}
.pagination{
 position: fixed;
    bottom: 25px;
    height: 40px;
    width: 940px;
    text-align: center;
}

</style>
